package com.docking.heylo.web.handler;



import com.docking.heylo.exception.GeneralException;
import com.docking.heylo.web.utils.ResultUtil;
import com.docking.heylo.web.vo.BaseVo;
import com.fasterxml.jackson.core.JsonParseException;
import io.jsonwebtoken.ExpiredJwtException;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.validation.ConstraintViolationException;

/**
 * Created by LiJ on 2018/5/8.
 */
@ControllerAdvice
public class DefaultExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(DefaultExceptionHandler.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public BaseVo handle(Exception e, HttpServletRequest request){
        e.printStackTrace();
        if(e instanceof GeneralException){                     //业务异常
            GeneralException ytxException = (GeneralException) e;
            return ResultUtil.error(-99, ytxException.getMessage());
        }else if(e instanceof BindException){               //参数传入异常
            BindException exception=(BindException)e;
            return ResultUtil.error(-98, exception.getFieldError().getDefaultMessage());
        }else if(e instanceof MethodArgumentNotValidException){//参数传入异常
            MethodArgumentNotValidException exception=(MethodArgumentNotValidException)e;
            BindingResult result=exception.getBindingResult();
            return ResultUtil.error(-97, result.getFieldError().getDefaultMessage());
        } else if(e instanceof NoHandlerFoundException) {
            String url=((NoHandlerFoundException) e).getRequestURL();
            return ResultUtil.error(-96,"未知的接口:"+url);
        } else if(e instanceof HttpRequestMethodNotSupportedException) {
            String method=((HttpRequestMethodNotSupportedException) e).getMethod();
            return ResultUtil.error(-95,"请求方式不正确:"+method);
        } else if( e instanceof MissingServletRequestParameterException) {
            String paramName=((MissingServletRequestParameterException) e).getParameterName();
            return ResultUtil.error(-94,"缺少参数:"+paramName);
        } else if(e.getCause() instanceof JsonParseException){
            return ResultUtil.error(-93,"json解析异常");
        } else if(e instanceof ConstraintViolationException){
            String msg=e.getMessage();
            return ResultUtil.error(-92,msg);
        } else if( e instanceof NumberFormatException){
            String msg = e.getMessage();
            return ResultUtil.error(-91,"类型转换错误:"+msg);
        }else if( e instanceof MultipartException){
            String msg = e.getMessage();
            return ResultUtil.error(-90,"文件大小受限:" + msg);
        } else if(e instanceof ExpiredJwtException){
            return ResultUtil.error(-2,"登录已过期，请重新登录");
        }else{
            logger.error("[系统异常]",e.getMessage());
            return ResultUtil.error(-1,"[系统异常]");
        }
    }
}
